function [NewW1,NewB1,NewW2,NewB2]=PSO_GATrain(SamIn,SamOut,HiddenUnitNum);
Maxgeneration=30000;
E0=0.001;
%Xmin=-10;
%Xmax=10;
Vmin=-120;
Vmax=120;
M=84;%���������������
% c1=2.7;
% c2=1.3;
c1=2;
c2=2;
w=0.9;
[R,SamNum]=size(SamIn);
[S2,SamNum]=size(SamOut);%5��4000
generation=1;
Done=0;
%��žֲ����Ž⣬��Ӧÿ�����ӻ���һ���ֲ����Ž⡣
Pb1=zeros(HiddenUnitNum,R,M);
Pb2=zeros(HiddenUnitNum,1,M);
Pb3=zeros(S2,1,M);
Pb4=zeros(S2,HiddenUnitNum,M);
%���ȫ�����Ž⣬��Ӧÿ�ε�����������һ����Ⱥ�壩����һ��ȫ�����Ž⡣ȥ���ڼ������ӵ��Ǹ�ά�ȡ�
Pg1=zeros(HiddenUnitNum,R);
Pg2=zeros(HiddenUnitNum,1);
Pg3=zeros(S2,1);
Pg4=zeros(S2,HiddenUnitNum);

E=zeros(size(SamOut));
rand('state',sum(100*clock));
%��ʼ��λ��
startP1=rand(HiddenUnitNum,R,M)-0.5;
startP2=rand(HiddenUnitNum,1,M)-0.5;
startP3=rand(S2,1,M)-0.5;
startP4=rand(S2,HiddenUnitNum,M)-0.5;
%��ʼ���ٶ�
startV1=rand(HiddenUnitNum,R,M)-0.5;
startV2=rand(HiddenUnitNum,1,M)-0.5;
startV3=rand(S2,1,M)-0.5;
startV4=rand(S2,HiddenUnitNum,M)-0.5;
%����ÿ����һ�κ��λ��
endP1=zeros(HiddenUnitNum,R,M);
endP2=zeros(HiddenUnitNum,1,M);
endP3=zeros(S2,1,M);
endP4=zeros(S2,HiddenUnitNum,M);
%����ÿ����һ�κ���ٶ�
endV1=zeros(HiddenUnitNum,R,M);
endV2=zeros(HiddenUnitNum,1,M);
endV3=zeros(S2,1,M);
endV4=zeros(S2,HiddenUnitNum,M);

startE=zeros(1,M);
endE=zeros(1,M);
NetworkOut=zeros(size(SamOut));
%����forend����������84�����ӡ���ÿ��λ�ô���ֲ����Ž⡣
for i=1:M
    W1=startP1(1:HiddenUnitNum,1:R,i);
    B1=startP2(1:HiddenUnitNum,1,i);
    %W2=startP2(1:HiddenUnitNum,1,i);
    B2=startP3(1:S2,1,i);
    %B2=startP4(1:S2,1:HiddenUnitNum,i);
    W2=startP4(1:S2,1:HiddenUnitNum,i);
    for q=1:SamNum
        TempOut=logsig(W1*SamIn(:,q)+B1);
        NetworkOut(:,q)=W2*TempOut+B2;
    end
    E=NetworkOut-SamOut;
    startE(1,i)=sumsqr(E)/(SamNum*S2);
    Pb1(:,:,i)=startP1(:,:,i);
    Pb2(:,:,i)=startP2(:,:,i);
    Pb3(:,:,i)=startP3(:,:,i);
    Pb4(:,:,i)=startP4(:,:,i);
end
%�ҵ������С���Ǹ����ӵ�����position��ȡ���Ǹ�λ�õ�����W��B�������һ�ε�ȫ�����Ž⡣
[val,position]=min(startE(1,:));
Pg1=startP1(:,:,position);
Pg2=startP2(:,:,position);
Pg3=startP3(:,:,position);
Pg4=startP4(:,:,position);
%Pgvalue����С���Ǹ����������ƽ���ͽ���ƽ����
Pgvalue=val;
%Pgvalue_last=Pgvalue;
ErrHistory=[];
while(~Done)%һ�ε�����
    for num=1:M
        endV1(:,:,num)=w*startV1(:,:,num)+c1*rand*(Pb1(:,:,num)-startP1(:,:,num))+c2*rand*(Pg1-startP1(:,:,num));
        endV2(:,:,num)=w*startV2(:,:,num)+c1*rand*(Pb2(:,:,num)-startP2(:,:,num))+c2*rand*(Pg2-startP2(:,:,num));
        endV3(:,:,num)=w*startV3(:,:,num)+c1*rand*(Pb3(:,:,num)-startP3(:,:,num))+c2*rand*(Pg3-startP3(:,:,num));
        endV4(:,:,num)=w*startV4(:,:,num)+c1*rand*(Pb4(:,:,num)-startP4(:,:,num))+c2*rand*(Pg4-startP4(:,:,num));
%startV1=rand(HiddenUnitNum,R,M);
%startV2=rand(HiddenUnitNum,1,M);
%startV3=rand(S2,1,M);
%startV4=rand(S2,HiddenUnitNum,M);
        for i=1:HiddenUnitNum
            for j=1:R
                %endV1(i,j,num)=endV1(i,j,num);
                if endV1(i,j,num)>Vmax
                    endV1(i,j,num)=Vmax;
                elseif endV1(i,j,num)<Vmin
                        endV1(i,j,num)=Vmin;
                end
            end
        end
        for i=1:HiddenUnitNum
            %endV2(i,1,num)=endV2(i,1,num);
            if endV2(i,1,num)>Vmax
               endV2(i,1,num)=Vmax;
            elseif endV2(i,1,num)<Vmin
                   endV2(i,1,num)=Vmin;
            end
        end
        for i=1:S2
            %endV3(i,1,num)=endV3(i,1,num);
            if endV3(i,1,num)>Vmax
                endV3(i,1,num)=Vmax;
            elseif endV3(i,1,num)<Vmin
                endV3(i,1,num)=Vmin;
            end
        end
        for i=1:S2
            for j=1:HiddenUnitNum
                %endV4(i,j,num)=endV4(i,j,num);
                if endV4(i,j,num)>Vmax
                    endV4(i,j,num)=Vmax;
                elseif endV4(i,j,num)<Vmin
                        endV4(i,j,num)=Vmin;
                end
            end
        end
        endP1(:,:,num)=startP1(:,:,num)+endV1(:,:,num);
        endP2(:,:,num)=startP2(:,:,num)+endV2(:,:,num);
        endP3(:,:,num)=startP3(:,:,num)+endV3(:,:,num);
        endP4(:,:,num)=startP4(:,:,num)+endV4(:,:,num);
%endP1=zeros(HiddenUnitNum,R,M);endP2=zeros(HiddenUnitNum,1,M);endP3=zeros(S2,1,M);endP4=zeros(S2,HiddenUnitNum,M);
%         for i=1:HiddenUnitNum
%             for j=1:R
%                 if endP1(i,j,num)>Xmax
%                    endP1(i,j,num)=Xmax;
%                 elseif endP1(i,j,num)<Xmin
%                        endP1(i,j,num)=Xmin;
%                 end
%             end
%         end
%         for i=1:HiddenUnitNum
%             if endP2(i,1,num)>Xmax
%                endP2(i,1,num)=Xmax;
%             elseif endP2(i,1,num)<Xmin
%                endP2(i,1,num)=Xmin;
%             end
%         end
%         for i=1:S2
%             if endP3(i,1,num)>Xmax
%                 endP3(i,1,num)=Xmax;
%             elseif endP3(i,1,num)<Xmin
%                 endP3(i,1,num)=Xmin;
%             end
%         end
%         for i=1:S2
%             for j=1:HiddenUnitNum
%                 if endP4(i,j,num)>Xmax
%                     endP4(i,j,num)=Xmax;
%                 elseif endP4(i,j,num)<Xmin
%                     endP4(i,j,num)=Xmin;
%                 end
%             end
%         end
        
%W1=startP1(1:HiddenUnitNum,1:R,i);B2=startP2(1:HiddenUnitNum,1,i);B1=startP3(1:S2,1,i);W2=startP4(1:S2,1:HiddenUnitNum,i);
        W1=endP1(1:HiddenUnitNum,1:R,num);
        W2=endP4(1:S2,1:HiddenUnitNum,num);
        B2=endP3(1:S2,1,num);
        B1=endP2(1:HiddenUnitNum,1,num);
        for q=1:SamNum
            TempOut=logsig(W1*SamIn(:,q)+B1);
            NetworkOut(:,q)=W2*TempOut+B2;
        end
        E=NetworkOut-SamOut;
        SSE=sumsqr(E)   %����������ڹ۲��������ı仯���
        ErrHistory=[ErrHistory SSE];
        endE(1,num)=SSE/(SamNum*S2);
        if endE(1,num)<startE(1,num)%����С���͸��¾ֲ����Ž⡣����ά��ԭ���ľֲ����Ž⡣
            Pb1(:,:,num)=endP1(:,:,num);
            Pb2(:,:,num)=endP2(:,:,num);
            Pb3(:,:,num)=endP3(:,:,num);
            Pb4(:,:,num)=endP4(:,:,num);
            startE(1,num)=endE(1,num);
        end
        startP1(:,:,num)=endP1(:,:,num);
        startP2(:,:,num)=endP2(:,:,num);
        startP3(:,:,num)=endP3(:,:,num);
        startP4(:,:,num)=endP4(:,:,num);
        startV1(:,:,num)=endV1(:,:,num);
        startV2(:,:,num)=endV2(:,:,num);
        startV3(:,:,num)=endV3(:,:,num);
        startV4(:,:,num)=endV4(:,:,num);
    end
    w=0.9-(0.5/Maxgeneration)*generation;
    [value,position]=min(startE(1,:));
    if value<Pgvalue
        Pg1=Pb1(:,:,position);
        Pg2=Pb2(:,:,position);
        Pg3=Pb3(:,:,position);
        Pg4=Pb4(:,:,position);
        Pgvalue=value;
    end
    if (generation>=Maxgeneration)
        Done=1;
    end
    if Pgvalue<E0
        Done=1;
    end
    generation=generation+1;
end
%     startP1=endP1;
%     startP2=endP2;
%     startP3=endP3;
%     startP4=endP4;
%     startV1=endV1;
%     startV2=endV2;
%     startV3=endV3;
%     startV4=endV4;
%     startE=endE;
W1=Pg1(1:HiddenUnitNum,1:R);
W2=Pg4(1:S2,1:HiddenUnitNum);
B2=Pg3(1:S2,1);
B1=Pg2(1:HiddenUnitNum,1);
NewW1=W1;
NewW2=W2;
NewB1=B1;
NewB2=B2;
%X=1:1:(generation-1);
% [LP,LPNum]=size(ErrHistory)
% X=1:1:LPNum;
% Y=ErrHistory;
% plot(X,Y)
%ErrHistory�ǣ�generation-1����84��������84��������Ŀ��
%���ֻ�ͼ��ʽ�������Ӧһ��һ��һ����generation����ӦErrHistoryҪ��84һ��һ�����ֵ��
%�������ֱ�ӻ���ErrHistory�����������ݣ�����������ErrHistory��size��

